# 导包
import pymysql


class DBTools(object):

    # 创建连接对象
    @staticmethod
    def __conn():
        conn = pymysql.Connect(host="121.43.169.97", port=3306,
                               user="student", password="P2P_student_2022",
                               database="czbk_member", charset="utf8")
        return conn

    @staticmethod
    def query_sql(sql):
        # 初始对象值
        my_conn = None
        cur = None
        try:
            # 调用方法获取连接对象
            my_conn = DBTools.__conn()
            # 创建游标
            cur = my_conn.cursor()
            # 执行sql语句
            cur.execute(sql)
            # 获取返回结果
            result = cur.fetchall()
            # 返回查询结果
            return result
        except Exception as e:
            return f"执行异常的结果:{e}"
        finally:
            # 关闭游标
            cur.close()
            # 关闭连接
            my_conn.close()

    @staticmethod
    def uid_sql(sql):
        # 初始对象值
        my_conn = None
        cur = None
        try:
            # 调用方法获取连接对象
            my_conn = DBTools.__conn()
            # 创建游标
            cur = my_conn.cursor()
            # 执行sql语句
            cur.execute(sql)
            # 提交实务
            my_conn.commit()
            # 返回查询结果
            return f"影响行数:{cur.rowcount}"
        except Exception as e:
            # 回滚事务
            my_conn.rollback()
            return f"执行异常的结果:{e}"
        finally:
            # 关闭游标
            cur.close()
            # 关闭连接
            my_conn.close()


if __name__ == '__main__':
    sql = """
    select * from mb_member where phone = "15012000001"
    """
    sql1 = """
    delete * from mb_member where phone between "15012000001" and "15012000006
    """
    # print(DBTools.query_sql(sql1))
    print(DBTools.uid_sql(sql))
